<?php

function draw($prize){
	$times = 10;
	$max = 0;

	foreach ($prize as $k => $v) {
		// 保证小数概率能落在随机整数中
	    $max = $v['rate'] * $times + $max;
	    $prizeZone[$k] = $max;
	}

	$max--; //临界值,确保随机数小于最大概率
	$rand = mt_rand(0, $max);

	foreach ($prizeZone as $k => $v) {
		if($rand < $v){
			return $k;
		}
	}
	
}

//最后确认相加等于100
$prize = [  
    0 => ['id'=>1, 'prize'=>'长江七号', 'rate'=>3.5],   
    1 => ['id'=>2, 'prize'=>'3Q币', 'rate'=>2.5],   
    2 => ['id'=>3, 'prize'=>'10000游戏币', 'rate'=>6],   
    3 => ['id'=>4, 'prize'=>'99999游戏币', 'rate'=>2],   
    4 => ['id'=>5, 'prize'=>'谢谢参与!', 'rate'=>42],   
    5 => ['id'=>6, 'prize'=>'欢迎下次再来！', 'rate'=>36],   
    6 => ['id'=>7, 'prize'=>'10Q币', 'rate'=>2],   
    7 => ['id'=>8, 'prize'=>'洗衣液', 'rate'=>4],   
    8 => ['id'=>9, 'prize'=>'纸巾一条', 'rate'=>2]  
]; 

$total = [];
foreach ($prize as $k => $v) {
	// 初始化每个概率统计次数为0
	$total[$k] = 0;
}

$i = 0;
$drawNum = 500;
// 测试500次实验后随机产生概率是否满足配置概率
while($i < $drawNum){
	$zone = draw($prize);
	// print_r($prize[$zone]['prize']);
	$total[$zone] += 1;
	$i++;
}

foreach ($total as $k => $v) {
	echo '配置概率为' . $prize[$k]['rate'], ',随机抽奖概率为' . number_format($v / $drawNum * 100, 1) . PHP_EOL;
}